package demo1;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:comparable接口
 * User: 石方旭
 * Date: 2022-04-11
 * Time: 13:28
 */
//
//class Student implements Comparable<Student>{
//    public String name;
//    public int age;
//    public double score;
//
//    public Student(String name, int age, double score) {
//        this.name = name;
//        this.age = age;
//        this.score = score;
//    }
//
//    @Override
//    public String toString() {
//        return "Student{" +
//                "name='" + name + '\'' +
//                ", age=" + age +
//                ", score=" + score +
//                '}';
//    }
//
////    @Override
////    public int compareTo(Student o) {
////        return this.age - o.age;
////    }
////    @Override
////    public int compareTo(Student o) {
////        return this.name.compareTo(o.name);
////    }
//    @Override
//    public int compareTo(Student o) {
//        return (int)(this.score - o.score);
//    }
//}
//
//public class TestDemo {
//    public static void main(String[] args) {
//        Student[] student = new Student[4];
//        student[0] = new Student("张三",18,88);
//        student[1] = new Student("李四",98,98);
//        student[2] = new Student("王二麻子",8,18);
//        student[3] = new Student("赵老八",58,38);
//        System.out.println(Arrays.toString(student));
//        Arrays.sort(student);
//        System.out.println(Arrays.toString(student));
//    }
//
//}

class Student implements Comparable<Student>{
    public String name;
    public int age;
    public double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return (int)(this.score - o.score);
    }
}
public class TestDemo {
    public static void main(String[] args) {
        Student[] student = new Student[4];
        student[0] = new Student("张三",18,88);
        student[1] = new Student("李四",98,98);
        student[2] = new Student("王二麻子",8,18);
        student[3] = new Student("赵老八",58,38);
        System.out.println(Arrays.toString(student));
        Arrays.sort(student);
        System.out.println(Arrays.toString(student));
    }
}
